/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.core;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.List;

/**
 * 物理路径树(Quick Query)<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public interface IPhysicalTree<Id extends Serializable, C extends IPhysicalTree<Id, C>>
    extends ITreeEntity<Id, C> {
  /**
   * 获取物化路径
   *
   * @return 物化路径
   */
  String getPhysicalPath();

  /**
   * 设置物化路径
   *
   * @param physicalPath 物理路径(For Linux)
   */
  void setPhysicalPath(String physicalPath);

  /**
   * 转换递归树数据结构
   *
   * @param parent 父节点
   * @param list 包含父节点的所有子节点/按照物化路径获取的所有子节点
   * @param <T> 支持树形结构的类型
   */
  static <T extends IPhysicalTree<?, T>> void recursion(IPhysicalTree<?, T> parent, List<T> list) {
    List<T> noCurrentParentChildes = Lists.newArrayList();
    List<T> childes = parent.getChildren() == null ? Lists.newArrayList() : parent.getChildren();

    if (CollectionUtil.isNotEmpty(list)) {
      for (T item : list) {
        if (parent.getId().equals(item.getParentId())) {
          childes.add(item);
        } else {
          noCurrentParentChildes.add(item);
        }
      }
    }

    parent.setChildren(childes.isEmpty() ? null : childes);

    if (CollectionUtil.isNotEmpty(childes) && CollectionUtil.isNotEmpty(noCurrentParentChildes)) {
      childes.forEach(c -> IPhysicalTree.recursion(c, noCurrentParentChildes));
    }
  }

  /**
   * 构建设置树物化路径
   *
   * @param pp 父节点路径{@link #getParentId()}.{@link #getPhysicalPath()}
   * @param cp 子节点路径(当前节点){@link #getPhysicalPath()}, 也可以是当前对象数据中的标识，因为父节点构建规则也是此方法
   */
  default void buildTreePhysicalPath(String pp, String cp) {
    String regex = "^/|/$", empty = "", p;
    Assert.isFalse(
        StrUtil.isBlank(pp) && StrUtil.isBlank(cp),
        "parent & children(current) node / path, cannot be both empty");
    if (StrUtil.isBlank(pp)) {
      p = StrUtil.format("/{}", cp.replaceAll(regex, empty));
    } else {
      p = StrUtil.format("/{}/{}", pp.replaceAll(regex, empty), cp.replaceAll(regex, empty));
    }
    this.setPhysicalPath(p);
  }
}
